package com.example.photoview;

import java.io.File;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Locale;

public class ImageList {
    //1.判断文件是否为图片
    public static boolean isSupportImage(String name) {
        String FileType = name.substring(name.lastIndexOf('.') + 1, name.length());
        if (FileType.equals(".jpg") || FileType.equals(".jepg") || FileType.equals(".gif") ||
                FileType.equals(".bmp") || FileType.equals(".png"))
            return true;
        else return false;
    }

    //2.初始化图片列表
    public static ArrayList<ImageModel> initImageList(String path) throws IOException {
        ArrayList<ImageModel> imageList = new ArrayList<>();
        if (path.equals("") || path == null)
            return null;
        Files.walkFileTree(Paths.get(path), new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
                String fileName = file.getFileName().toString().toLowerCase();
                if (isSupportImage(fileName)) {
                    imageList.add(new ImageModel(file.toString()));
                }
                return FileVisitResult.CONTINUE;
            }
        });
        return imageList;


    }
    //3. 返回文件夹内图片张数
    public static int getListImgNum(ArrayList<ImageModel> im) {
        return im.size();
    }

    // 4.返回文件夹内的 图片 大小
    public static String getListImgSize(ArrayList<ImageModel> im) {
        long total = 0;
        for (ImageModel i : im) {
            total += i.getFileLength();
        }
        return Model.getFormLength(total);
    }
    //5.刷新界面
    public static ArrayList<ImageModel> refreshList(String path){
        ArrayList<ImageModel> list;
        try{
            list=initImageList(path);
        }catch (IOException e){
           return null;
        }
        return list;
    }
    //6.按排序刷新界面
    public static ArrayList<ImageModel> refresh(String path,String mode){
        ArrayList<ImageModel>list=refreshList(path);
        switch(mode){
            case SortParam.SBND:
                assert list != null;
                Collections.reverse(list);//降序排序
                return list;
            case SortParam.SBSR:
                assert list != null;
                list.sort(new SortBySize());
                return list;
            case SortParam.SBSD:
                assert list != null;
                list.sort(new SortBySize());
                Collections.reverse(list);
                return list;
            case SortParam.SBDR:
                assert list != null;
                list.sort(new SortByDate());
                return list;
            case SortParam.SBDD:
                assert list != null;
                list.sort(new SortByDate());
                Collections.reverse(list);
                return list;
        }
        return list;
    }
}